package com.shengsiyuan.nio;

import java.nio.IntBuffer;
import java.security.SecureRandom;

/**
 * Created by deng_wb on 2019/8/14
 */
public class NioTest1 {

    public static void main(String[] args) {
        IntBuffer buffer = IntBuffer.allocate(10);
        System.out.println("init: "+"position: "+buffer.position()+" limit: "+buffer.limit()+" capacity: "+buffer.capacity());


        for (int i = 0; i < 5; i++) {
            int randomNumber = new SecureRandom().nextInt(20);
            buffer.put(randomNumber);
        }

        System.out.println("before flip: "+"position: "+buffer.position()+" limit: "+buffer.limit()+" capacity: "+buffer.capacity());
        buffer.flip(); //状态反转,实现读写切换
        System.out.println("after flip: "+"position: "+buffer.position()+" limit: "+buffer.limit()+" capacity: "+buffer.capacity());
        while (buffer.hasRemaining()){
            int i = buffer.get();
            System.out.println("loop: position: "+buffer.position()+" limit: "+buffer.limit()+" capacity: "+buffer.capacity());
//            System.out.println(i);
        }
        System.out.println("end: position: "+buffer.position()+" limit: "+buffer.limit()+" capacity: "+buffer.capacity());

    }
}
